//
//  NewFeatureController.swift
//  微博001
//
//  Created by apple on 15/12/16.
//  Copyright © 2015年 apple. All rights reserved.
//

import UIKit
import SnapKit

private let imageCount = 4

private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {
    
    //不需要添加override
    //构造方法 方法只能向上 继承一个层级, 不能够过父亲 找爷爷的构造方法
    
    init() {
        //调用指定的构造方法,  是i
        //指定流水布局
        let layout = UICollectionViewFlowLayout()
        //修改布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        //修改视图滚动方向
        layout.scrollDirection = .Horizontal
        //设置item的上下左右的间距
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        //super.init 是构造的最后一步
        super.init(collectionViewLayout: layout)
        //分页效果,分页效果是scrollView的属性
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
        
        collectionView?.showsHorizontalScrollIndicator = false
        
        
        
        
    }
   //如果当前控视图控制器通过xib/sb加载  程序就会崩溃
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    
    
    
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()

      
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: UICollectionViewDataSource
//默认一组
//    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
//        // #warning Incomplete implementation, return the number of sections
//        return 0
//    }


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return 4
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell

//        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.blueColor()
        cell.index = indexPath.item
  
        return cell
    }
    
    //cell在销毁的调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //cell完成展示后 indexPath 对应的是上一个cell indexPath 不能使用indexPath 来获取cell
          print(indexPath.item)
        //获取准确的cell
        let cell = collectionView.visibleCells().last! as! NewFeatureCell
        //获取cell的indexPath
        let index = collectionView.indexPathForCell(cell)!
        
        //判断index.item是不是等于3  执行动画
        if index.item == imageCount - 1 {
            //执行动画
            cell.showAnimation()
        }
      
        
        
        
    }

}


//在一个文件中 可以同时建多个类
class NewFeatureCell: UICollectionViewCell {
    


    //添加属性
    var index: Int = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(index + 1)")
            
            //隐藏按钮
            startBtn.hidden = index != 3
        }
    }
    
    
    @objc private func startBtnDidClick() {
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootVCNotification, object: nil)
    }
    
    //开始按钮的执行动画
   private func showAnimation() {
    
    //在动画开始之前,修改transform
    startBtn.transform = CGAffineTransformMakeScale(0, 0)
    
    
    UIView.animateWithDuration(1.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: [], animations: { () -> Void in
        self.startBtn.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            print("OK")
    }

    
    
    }
    
    //重写构造方法,  cell 构造的入口
    override init(frame: CGRect) {
        super.init(frame: frame)
//        print(frame)
          setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
//设置界面
    private func setupUI() {
        
        //添加子视图
        //自定义的子视图  需要添加在contentView上
        contentView.addSubview(iconView)
        contentView.addSubview(startBtn)
        
        //使用SnapKit 添加约束
//        snp_makeConstraints表示添加约束
        iconView.snp_makeConstraints { (make) -> Void in
            //make表示需要添加约束的对象
            make.edges.equalTo(contentView.snp_edges)
        }
        
        //设置开始按钮的约束
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView.snp_centerX)
            
            make.bottom.equalTo(contentView.snp_bottom).offset(-180)
        }
        
        
        //添加点击事件
        startBtn.addTarget(self, action: "startBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        
    }
    
    
    //分析视图中的所有子视图  将所有的子视图懒加载出来
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    
    //懒加载开始按钮
    private lazy var startBtn: UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        btn.setTitle("开始体验", forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        
        //设置大小
        btn.sizeToFit()
        return btn
        }()

}






















